package com.buildings.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.buildings.R;
import com.buildings.bean.LocalBean;
import com.buildings.bean.TableBean;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class LocalAdapter extends RecyclerView.Adapter<LocalAdapter.ViewHolder> {

    private List<LocalBean> data;
    private Activity activity;

    public LocalAdapter(Activity activity, List<LocalBean> data) {
        this.data = data;
        this.activity = activity;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_local_task, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.tvName.setText(data.get(position).getTitle());
        //修改任务
        holder.btnSubmit.setVisibility(View.GONE);
        //执行任务
        holder.btnEdit.setVisibility(View.GONE);
        //查看表单
        holder.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener)
                listener.onItemClick(position);
            }
        });
        holder.tv_usb_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener)
                    listener.onDelete(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return data != null ? data.size() : 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView btnSubmit;//修改任务
        TextView btnEdit;//执行任务
        TextView btnDelete;//查看表单
        TextView tvName;//查看表单
        Button tv_usb_delete;//查看表单

        public ViewHolder(View itemView) {
            super(itemView);
            btnSubmit = itemView.findViewById(R.id.btn_submit);
            btnEdit = itemView.findViewById(R.id.btn_edit);
            btnDelete = itemView.findViewById(R.id.btn_delete);
            tvName = itemView.findViewById(R.id.tv_name);
            tv_usb_delete = itemView.findViewById(R.id.tv_usb_delete);
        }
    }

    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(int position);
        void onDelete(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

}
